<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/5.13.0/css/all.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"horsenliu.gitee.io","root":"/blog/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","width":240,"display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"default"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="1. 信息命令1.1 查看版本 version1docker version  1.2 查看系统信息 info1docker info # 包括镜像数、容器数等等  1.3 获取帮助 help12docker --helpdocker -h  2. 镜像命令2.1 列出本地主机上的镜像 images1docker images [OPTIONS]     参数 含义    -a 列出所有镜像">
<meta property="og:type" content="article">
<meta property="og:title" content="03_常用命令">
<meta property="og:url" content="http://horsenliu.gitee.io/blog/posts/f629b0e6.html">
<meta property="og:site_name" content="霍森的笔记本">
<meta property="og:description" content="1. 信息命令1.1 查看版本 version1docker version  1.2 查看系统信息 info1docker info # 包括镜像数、容器数等等  1.3 获取帮助 help12docker --helpdocker -h  2. 镜像命令2.1 列出本地主机上的镜像 images1docker images [OPTIONS]     参数 含义    -a 列出所有镜像">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://horsenliu.gitee.io/blog/blog/posts/blog/posts/f629b0e6/1.jpg">
<meta property="article:published_time" content="2021-07-20T08:04:00.000Z">
<meta property="article:modified_time" content="2021-08-02T09:49:14.093Z">
<meta property="article:author" content="Horsen Liu">
<meta property="article:tag" content="Docker">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://horsenliu.gitee.io/blog/blog/posts/blog/posts/f629b0e6/1.jpg">

<link rel="canonical" href="http://horsenliu.gitee.io/blog/posts/f629b0e6.html">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>03_常用命令 | 霍森的笔记本</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">霍森的笔记本</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">记的不少 会的不多</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">123</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">36</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">220</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <a href="https://github.com/horsenliu" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://horsenliu.gitee.io/blog/posts/f629b0e6.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.png">
      <meta itemprop="name" content="Horsen Liu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="霍森的笔记本">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          03_常用命令
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-07-20 16:04:00" itemprop="dateCreated datePublished" datetime="2021-07-20T16:04:00+08:00">2021-07-20</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/%E5%B8%B8%E7%94%A8%E7%BB%84%E4%BB%B6%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" itemprop="url" rel="index"><span itemprop="name">常用组件学习笔记</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/%E5%B8%B8%E7%94%A8%E7%BB%84%E4%BB%B6%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/Docker/" itemprop="url" rel="index"><span itemprop="name">Docker</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>7.1k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>6 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="1-信息命令"><a href="#1-信息命令" class="headerlink" title="1. 信息命令"></a>1. 信息命令</h3><h4 id="1-1-查看版本-version"><a href="#1-1-查看版本-version" class="headerlink" title="1.1 查看版本 version"></a>1.1 查看版本 version</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker version</span><br></pre></td></tr></table></figure>

<h4 id="1-2-查看系统信息-info"><a href="#1-2-查看系统信息-info" class="headerlink" title="1.2 查看系统信息 info"></a>1.2 查看系统信息 info</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker info <span class="comment"># 包括镜像数、容器数等等</span></span><br></pre></td></tr></table></figure>

<h4 id="1-3-获取帮助-help"><a href="#1-3-获取帮助-help" class="headerlink" title="1.3 获取帮助 help"></a>1.3 获取帮助 help</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker --<span class="built_in">help</span></span><br><span class="line">docker -h</span><br></pre></td></tr></table></figure>

<h3 id="2-镜像命令"><a href="#2-镜像命令" class="headerlink" title="2. 镜像命令"></a>2. 镜像命令</h3><h4 id="2-1-列出本地主机上的镜像-images"><a href="#2-1-列出本地主机上的镜像-images" class="headerlink" title="2.1 列出本地主机上的镜像 images"></a>2.1 列出本地主机上的镜像 images</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker images [OPTIONS]</span><br></pre></td></tr></table></figure>

<table>
<thead>
<tr>
<th>参数</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td><code>-a</code></td>
<td>列出所有镜像</td>
</tr>
<tr>
<td><code>-q</code></td>
<td>只显示镜像ID</td>
</tr>
<tr>
<td><code>--digests</code></td>
<td>显示镜像摘要信息</td>
</tr>
</tbody></table>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[root@horsneliu ~]<span class="comment"># docker images</span></span><br><span class="line"><span class="comment"># 仓库源       # 标签    # 镜像ID        # 创建时间      # 大小</span></span><br><span class="line">REPOSITORY    TAG       IMAGE ID       CREATED        SIZE</span><br><span class="line">hello-world   latest    d1165f221234   4 months ago   13.3kB</span><br></pre></td></tr></table></figure>

<span id="more"></span>

<h4 id="2-2-搜索镜像-search"><a href="#2-2-搜索镜像-search" class="headerlink" title="2.2 搜索镜像 search"></a>2.2 搜索镜像 search</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker search [OPTIONS] 镜像名</span><br></pre></td></tr></table></figure>

<table>
<thead>
<tr>
<th>参数</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td><code>--filter stars=50</code></td>
<td>列出收藏数不小于 50 的镜像</td>
</tr>
<tr>
<td><code>--limit 10</code></td>
<td>列出前 10 条</td>
</tr>
</tbody></table>
<p>也可以在网页上搜索<a target="_blank" rel="noopener" href="https://hub.docker.com/">https://hub.docker.com/</a></p>
<h4 id="2-3-下载镜像-pull"><a href="#2-3-下载镜像-pull" class="headerlink" title="2.3 下载镜像 pull"></a>2.3 下载镜像 pull</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker pull [OPTIONS] 镜像名 <span class="comment"># 不加tag默认是latest</span></span><br></pre></td></tr></table></figure>
<table>
<thead>
<tr>
<th>参数</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td><code>-a</code></td>
<td>下载所有 tag 镜像</td>
</tr>
<tr>
<td><code>-q</code></td>
<td>静默下载</td>
</tr>
</tbody></table>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">[root@horsneliu ~]<span class="comment"># docker pull mysql:5.7</span></span><br><span class="line">5.7: Pulling from library/mysql <span class="comment"># 根据tag下载</span></span><br><span class="line">b4d181a07f80: Pull complete <span class="comment"># 分层下载 联合文件系统</span></span><br><span class="line">a462b60610f5: Pull complete</span><br><span class="line">578fafb77ab8: Pull complete</span><br><span class="line">524046006037: Pull complete</span><br><span class="line">d0cbe54c8855: Pull complete</span><br><span class="line">aa18e05cc46d: Pull complete</span><br><span class="line">fd6f649b1d0a: Pull complete</span><br><span class="line">8a2b858b000b: Pull complete</span><br><span class="line">322182b17422: Downloading [===============================&gt;                   ]  67.82MB/108.6MB</span><br><span class="line">070e28050a88: Download complete</span><br><span class="line">613bdfd8796e: Download complete</span><br><span class="line">Digest: sha256:956e11ac581cad9ac8747a9a1d61b8ffcfa6845e0f23bdbab6ba20a2ad792cbf <span class="comment"># 签名</span></span><br><span class="line">Status: Downloaded newer image <span class="keyword">for</span> mysql:5.7</span><br><span class="line">docker.io/library/mysql:5.7 <span class="comment"># 真实位置</span></span><br></pre></td></tr></table></figure>

<h4 id="2-4-删除镜像-rmi"><a href="#2-4-删除镜像-rmi" class="headerlink" title="2.4 删除镜像 rmi"></a>2.4 删除镜像 rmi</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">docker rmi [OPTIONS] 镜像名</span><br><span class="line"></span><br><span class="line">docker rmi -f 镜像ID <span class="comment"># 删除单个</span></span><br><span class="line">docker rmi -f 镜像名:tag 镜像名:tag <span class="comment"># 删除多个</span></span><br><span class="line">docker rmi -f $(docker images -qa) <span class="comment"># 删除全部</span></span><br></pre></td></tr></table></figure>

<table>
<thead>
<tr>
<th>参数</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td><code>-f</code></td>
<td>强制删除</td>
</tr>
</tbody></table>
<h3 id="3-容器命令"><a href="#3-容器命令" class="headerlink" title="3. 容器命令"></a>3. 容器命令</h3><p>只有镜像才能创建容器。先搞一个 centos 的镜像：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker pull centos</span><br></pre></td></tr></table></figure>

<h4 id="3-1-新建容器并启动-run"><a href="#3-1-新建容器并启动-run" class="headerlink" title="3.1 新建容器并启动 run"></a>3.1 新建容器并启动 run</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run [OPTIONS] 镜像名</span><br></pre></td></tr></table></figure>

<table>
<thead>
<tr>
<th>参数</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td><code>--name &quot;name&quot;</code></td>
<td>给容器指定一个名字</td>
</tr>
<tr>
<td><code>-d</code></td>
<td>后台方式运行容器，并返回容器ID</td>
</tr>
<tr>
<td><code>-i</code></td>
<td>以交互模式运行容器（通常和 -t 一起用）</td>
</tr>
<tr>
<td><code>-t</code></td>
<td>给容器分配一个终端（通常和 -i 一起用）</td>
</tr>
<tr>
<td><code>-P</code></td>
<td>随机端口映射</td>
</tr>
<tr>
<td><code>-p</code></td>
<td>指定端口映射</td>
</tr>
</tbody></table>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 使用centos进行交互模式启动容器，在容器内执行/bin/bash命令</span></span><br><span class="line">[root@horsneliu ~]<span class="comment"># docker run -it centos /bin/bash</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 这个时候已经切换到容器内部了，主机名就是容器ID</span></span><br><span class="line">[root@c486b9e91d69 /]<span class="comment"># ls</span></span><br><span class="line">bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var</span><br><span class="line"></span><br><span class="line"><span class="comment"># 退出容器</span></span><br><span class="line">[root@c486b9e91d69 /]<span class="comment"># exit</span></span><br><span class="line"><span class="built_in">exit</span></span><br></pre></td></tr></table></figure>

<h4 id="3-2-列出运行中的容器-ps"><a href="#3-2-列出运行中的容器-ps" class="headerlink" title="3.2 列出运行中的容器 ps"></a>3.2 列出运行中的容器 ps</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker ps [OPTIONS]</span><br></pre></td></tr></table></figure>

<table>
<thead>
<tr>
<th>参数</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td><code>-a</code></td>
<td>列出当前所有正在运行的容器和历史运行过的容器</td>
</tr>
<tr>
<td><code>-l</code></td>
<td>显示最近创建的容器</td>
</tr>
<tr>
<td><code>-n ?</code></td>
<td>显示最近n个创建的容器</td>
</tr>
<tr>
<td><code>-q</code></td>
<td>静默模式，只显示容器编号</td>
</tr>
</tbody></table>
<h4 id="3-3-退出容器-exit"><a href="#3-3-退出容器-exit" class="headerlink" title="3.3 退出容器 exit"></a>3.3 退出容器 exit</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">exit</span> <span class="comment"># 停止容器并退出</span></span><br><span class="line">ctrl+p+q <span class="comment"># 不停止容器退出</span></span><br></pre></td></tr></table></figure>

<h4 id="3-4-进入正在运行的容器-exec-attach"><a href="#3-4-进入正在运行的容器-exec-attach" class="headerlink" title="3.4 进入正在运行的容器 exec attach"></a>3.4 进入正在运行的容器 exec attach</h4><h5 id="3-4-1-方式一：在容器中打开新的终端（启动新的进程）"><a href="#3-4-1-方式一：在容器中打开新的终端（启动新的进程）" class="headerlink" title="3.4.1 方式一：在容器中打开新的终端（启动新的进程）"></a>3.4.1 方式一：在容器中打开新的终端（启动新的进程）</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker <span class="built_in">exec</span> -it 容器ID COMMAND</span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">[root@horsneliu ~]<span class="comment"># docker exec -it 434aebe93803 /bin/bash</span></span><br><span class="line">[root@434aebe93803 /]<span class="comment"># ps -ef</span></span><br><span class="line">UID        PID  PPID  C STIME TTY          TIME CMD</span><br><span class="line">root         1     0  0 Jul20 ?        00:00:49 /bin/sh -c <span class="keyword">while</span> <span class="literal">true</span>;<span class="keyword">do</span> <span class="built_in">echo</span> horsenliu;<span class="built_in">sleep</span> 1;<span class="keyword">done</span></span><br><span class="line">root     28131     0  0 07:44 pts/0    00:00:00 /bin/bash</span><br><span class="line">root     28163     1  0 07:44 ?        00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=<span class="built_in">sleep</span> /usr/bin/sleep 1</span><br><span class="line">root     28164 28131  0 07:44 pts/0    00:00:00 ps -ef</span><br></pre></td></tr></table></figure>

<h5 id="3-4-2-方式二：直接进入容器启动命令的终端（不启动新的进程）"><a href="#3-4-2-方式二：直接进入容器启动命令的终端（不启动新的进程）" class="headerlink" title="3.4.2 方式二：直接进入容器启动命令的终端（不启动新的进程）"></a>3.4.2 方式二：直接进入容器启动命令的终端（不启动新的进程）</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker attach 容器ID</span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[root@horsneliu ~]<span class="comment"># docker attach 434aebe93803</span></span><br><span class="line">horsenliu</span><br><span class="line">horsenliu</span><br><span class="line">horsenliu</span><br><span class="line">horsenliu</span><br><span class="line">horsenliu</span><br></pre></td></tr></table></figure>

<h4 id="3-5-启动-x2F-停止容器-start"><a href="#3-5-启动-x2F-停止容器-start" class="headerlink" title="3.5 启动&#x2F;停止容器 start"></a>3.5 启动&#x2F;停止容器 start</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">docker start 容器ID或容器名 <span class="comment"># 启动容器</span></span><br><span class="line">docker restart 容器ID或容器名 <span class="comment"># 重启容器</span></span><br><span class="line">docker stop 容器ID或容器名 <span class="comment"># 停止容器</span></span><br><span class="line">docker <span class="built_in">kill</span> 容器ID或容器名 <span class="comment"># 强制停止容器</span></span><br></pre></td></tr></table></figure>

<h4 id="3-6-删除容器-rm"><a href="#3-6-删除容器-rm" class="headerlink" title="3.6 删除容器 rm"></a>3.6 删除容器 rm</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">docker <span class="built_in">rm</span> 容器ID <span class="comment"># 删除指定容器</span></span><br><span class="line">docker <span class="built_in">rm</span> -f $(docker ps -aq) <span class="comment"># 删除所有容器</span></span><br><span class="line">docker ps -aq|xargs docker <span class="built_in">rm</span> <span class="comment"># 删除所有容器</span></span><br></pre></td></tr></table></figure>

<h3 id="4-其他命令"><a href="#4-其他命令" class="headerlink" title="4. 其他命令"></a>4. 其他命令</h3><h4 id="4-1-后台启动容器"><a href="#4-1-后台启动容器" class="headerlink" title="4.1 后台启动容器"></a>4.1 后台启动容器</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -d 容器名</span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 后台启动centos的容器</span></span><br><span class="line">[root@horsneliu ~]<span class="comment"># docker run -d centos</span></span><br><span class="line">27b43f5cbba5330000e5dfc3c7008b74876351b072676a82741974063e3fec4e</span><br><span class="line"></span><br><span class="line"><span class="comment"># 查看正在运行的容器 但是没有</span></span><br><span class="line">[root@horsneliu ~]<span class="comment"># docker ps</span></span><br><span class="line">CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES</span><br></pre></td></tr></table></figure>

<p>docker 容器在后台运行，就必须有一个前台进程，容器运行的命令如果不是那些一直挂起的命令，就会自动退出。</p>
<h4 id="4-2-查看日志-logs"><a href="#4-2-查看日志-logs" class="headerlink" title="4.2 查看日志 logs"></a>4.2 查看日志 logs</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker logs [OPTIONS] 容器ID</span><br></pre></td></tr></table></figure>

<table>
<thead>
<tr>
<th>参数</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td><code>-f</code></td>
<td>实时打印日志</td>
</tr>
<tr>
<td><code>-t</code></td>
<td>显示时间戳</td>
</tr>
<tr>
<td><code>--tail 20</code></td>
<td>显示20条日志</td>
</tr>
</tbody></table>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">[root@horsneliu ~]<span class="comment"># docker run -d centos /bin/sh -c &quot;while true;do echo horsenliu;sleep 1;done&quot;</span></span><br><span class="line">434aebe93803cbc891e6657d8892f595ecc51ef931af0faa99e34de56e06b5ca</span><br><span class="line">[root@horsneliu ~]<span class="comment"># docker ps</span></span><br><span class="line">CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES</span><br><span class="line">434aebe93803   centos    <span class="string">&quot;/bin/sh -c &#x27;while t…&quot;</span>   8 seconds ago   Up 7 seconds             elegant_satoshi</span><br><span class="line">[root@horsneliu ~]<span class="comment"># docker logs -ft --tail 5 434aebe93803</span></span><br><span class="line">2021-07-20T11:45:45.058546051Z horsenliu</span><br><span class="line">2021-07-20T11:45:46.060748390Z horsenliu</span><br><span class="line">2021-07-20T11:45:47.062871992Z horsenliu</span><br><span class="line">2021-07-20T11:45:48.065099954Z horsenliu</span><br><span class="line">2021-07-20T11:45:48.065099954Z horsenliu</span><br><span class="line">2021-07-20T11:45:48.065099954Z horsenliu</span><br></pre></td></tr></table></figure>

<h4 id="4-3-查看容器中运行的进程信息-top"><a href="#4-3-查看容器中运行的进程信息-top" class="headerlink" title="4.3 查看容器中运行的进程信息 top"></a>4.3 查看容器中运行的进程信息 top</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker top 容器ID</span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[root@horsneliu ~]<span class="comment"># docker top 434aebe93803</span></span><br><span class="line">UID        PID        PPID       C       STIME      TTY        TIME</span><br><span class="line">root       8311       8292       0       Jul20      ?          00:00:48</span><br><span class="line">root       11055      8311       0       15:18      ?          00:00:00 </span><br></pre></td></tr></table></figure>

<h4 id="4-4-查看容器-x2F-镜像的元数据-inspect"><a href="#4-4-查看容器-x2F-镜像的元数据-inspect" class="headerlink" title="4.4 查看容器&#x2F;镜像的元数据 inspect"></a>4.4 查看容器&#x2F;镜像的元数据 inspect</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker inspect 容器ID</span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">[root@horsneliu ~]<span class="comment"># docker inspect 434aebe93803</span></span><br><span class="line">[</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="string">&quot;Id&quot;</span>: <span class="string">&quot;434aebe93803cbc891e6657d8892f595ecc51ef931af0faa99e34de56e06b5ca&quot;</span>,</span><br><span class="line">        <span class="string">&quot;Created&quot;</span>: <span class="string">&quot;2021-07-20T11:45:31.727328773Z&quot;</span>,</span><br><span class="line">        <span class="string">&quot;Path&quot;</span>: <span class="string">&quot;/bin/sh&quot;</span>,</span><br><span class="line">        <span class="string">&quot;Args&quot;</span>: [</span><br><span class="line">            <span class="string">&quot;-c&quot;</span>,</span><br><span class="line">            <span class="string">&quot;while true;do echo horsenliu;sleep 1;done&quot;</span></span><br><span class="line">        ],</span><br><span class="line">        <span class="string">&quot;State&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;Status&quot;</span>: <span class="string">&quot;running&quot;</span>,</span><br><span class="line">            <span class="string">&quot;Running&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">            <span class="string">&quot;Paused&quot;</span>: <span class="literal">false</span>,</span><br><span class="line">            <span class="string">&quot;Restarting&quot;</span>: <span class="literal">false</span>,</span><br><span class="line">            <span class="string">&quot;OOMKilled&quot;</span>: <span class="literal">false</span>,</span><br><span class="line">            <span class="string">&quot;Dead&quot;</span>: <span class="literal">false</span>,</span><br><span class="line">            <span class="string">&quot;Pid&quot;</span>: 8311,</span><br><span class="line">            <span class="string">&quot;ExitCode&quot;</span>: 0,</span><br><span class="line">            <span class="string">&quot;Error&quot;</span>: <span class="string">&quot;&quot;</span>,</span><br><span class="line">            <span class="string">&quot;StartedAt&quot;</span>: <span class="string">&quot;2021-07-20T11:45:32.008019107Z&quot;</span>,</span><br><span class="line">            <span class="string">&quot;FinishedAt&quot;</span>: <span class="string">&quot;0001-01-01T00:00:00Z&quot;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="string">&quot;Image&quot;</span>: <span class="string">&quot;sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55&quot;</span>,</span><br><span class="line">        <span class="string">&quot;ResolvConfPath&quot;</span>: <span class="string">&quot;/www/server/docker/containers/434aebe93803cbc891e6657d8892f595ecc51ef931af0faa99e34de56e06b5ca/resolv.conf&quot;</span>,</span><br><span class="line">        <span class="string">&quot;HostnamePath&quot;</span>: <span class="string">&quot;/www/server/docker/containers/434aebe93803cbc891e6657d8892f595ecc51ef931af0faa99e34de56e06b5ca/hostname&quot;</span>,</span><br><span class="line">        ......</span><br></pre></td></tr></table></figure>

<h4 id="4-5-从容器内拷贝文件到主机-cp"><a href="#4-5-从容器内拷贝文件到主机-cp" class="headerlink" title="4.5 从容器内拷贝文件到主机 cp"></a>4.5 从容器内拷贝文件到主机 cp</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker <span class="built_in">cp</span> 容器ID:容器内路径 主机目的路径</span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 进入容器</span></span><br><span class="line">[root@horsneliu /]<span class="comment"># docker attach 359beeb988ea</span></span><br><span class="line"><span class="comment"># 新建一个文件</span></span><br><span class="line">[root@359beeb988ea /]<span class="comment"># cd home/</span></span><br><span class="line">[root@359beeb988ea home]<span class="comment"># mkdir horsen</span></span><br><span class="line">[root@359beeb988ea home]<span class="comment"># cd horsen/</span></span><br><span class="line">[root@359beeb988ea horsen]<span class="comment"># touch hello.txt</span></span><br><span class="line"><span class="comment"># 退出来之后拷贝到主机</span></span><br><span class="line">[root@horsneliu /]<span class="comment"># docker cp 359beeb988ea:/home/horsen/hello.txt /home/horsen</span></span><br><span class="line"><span class="comment"># 看看有没有</span></span><br><span class="line">[root@horsneliu /]<span class="comment"># cd /home/horsen/</span></span><br><span class="line">[root@horsneliu horsen]<span class="comment"># ls</span></span><br><span class="line">hello.txt</span><br></pre></td></tr></table></figure>

<h3 id="5-小结"><a href="#5-小结" class="headerlink" title="5. 小结"></a>5. 小结</h3><img src="/blog/blog/posts/blog/posts/f629b0e6/1.jpg" class>

<table>
<thead>
<tr>
<th>命令</th>
<th>描述</th>
<th>翻译</th>
</tr>
</thead>
<tbody><tr>
<td>attach</td>
<td>Attach to a running container</td>
<td>连接到一个运行中的容器</td>
</tr>
<tr>
<td>build</td>
<td>Build an image from a Dockerfile</td>
<td>通过 Dockerfile 定制镜像</td>
</tr>
<tr>
<td>commit</td>
<td>Create a new image from a container changes</td>
<td>提交当前容器为新的镜像</td>
</tr>
<tr>
<td>cp</td>
<td>Copy files&#x2F;folders from the containers filesystem to the host path</td>
<td>容器中拷贝指定文件或者目录到宿主机中</td>
</tr>
<tr>
<td>create</td>
<td>Create a new container</td>
<td>创建一个新的容器，同 run，但不启动容器</td>
</tr>
<tr>
<td>diff</td>
<td>Inspect changes on a container’s filesystem</td>
<td>查看 docker 容器变化</td>
</tr>
<tr>
<td>events</td>
<td>Get real time events from the server</td>
<td>从 docker 服务获取容器实时事件</td>
</tr>
<tr>
<td>exec</td>
<td>Run a command in an existing container</td>
<td>在已存在的容器上运行命 令</td>
</tr>
<tr>
<td>export</td>
<td>Stream the contents of a container as a tar archive</td>
<td>导出容器的内 容流作为一个 tar 归档文件 [对应 import ]</td>
</tr>
<tr>
<td>history</td>
<td>Show the history of an image</td>
<td>展示一个镜像形成历史</td>
</tr>
<tr>
<td>images</td>
<td>List images</td>
<td>列出系统当前镜像</td>
</tr>
<tr>
<td>import</td>
<td>Create a new filesystem image from the contents of a tarball</td>
<td>从 tar 包中的内容创建一个新的文件系统映像 [对应 export]</td>
</tr>
<tr>
<td>info</td>
<td>Display system-wide information</td>
<td>显示系统相关信息</td>
</tr>
<tr>
<td>inspect</td>
<td>Return low-level information on a container</td>
<td>查看容器详细信息</td>
</tr>
<tr>
<td>kill</td>
<td>Kill a running container</td>
<td>kill 指定 docker 容 器</td>
</tr>
<tr>
<td>load</td>
<td>Load an image from a tar archive</td>
<td>从一个 tar 包中加载一 个镜像 [对应 save]</td>
</tr>
<tr>
<td>login</td>
<td>Register or Login to the docker registry server</td>
<td>注册或者登陆一个 docker 源服务器</td>
</tr>
<tr>
<td>logout</td>
<td>Log out from a Docker registry server</td>
<td>从当前 Docker registry 退出</td>
</tr>
<tr>
<td>logs</td>
<td>Fetch the logs of a container</td>
<td>输出当前容器日志信息</td>
</tr>
<tr>
<td>port</td>
<td>Lookup the public-facing port which is NAT-ed to PRIVATE_PORT</td>
<td>查看映射端口对应的容器内部源端口</td>
</tr>
<tr>
<td>pause</td>
<td>Pause all processes within a container</td>
<td>暂停容器</td>
</tr>
<tr>
<td>ps</td>
<td>List containers</td>
<td>列出容器列表</td>
</tr>
<tr>
<td>pull</td>
<td>Pull an image or a repository from the docker registry server</td>
<td>从 docker 镜像源服务器拉取指定镜像或者库镜像</td>
</tr>
<tr>
<td>push</td>
<td>Push an image or a repository to the docker registry server</td>
<td>推送指定镜像或者库镜像至 docker 源服务器</td>
</tr>
<tr>
<td>restart</td>
<td>Restart a running container</td>
<td>重启运行的容器</td>
</tr>
<tr>
<td>rm</td>
<td>Remove one or more containers</td>
<td>移除一个或者多个容器</td>
</tr>
<tr>
<td>rmi</td>
<td>Remove one or more images</td>
<td>移除一个或多个镜像 [无容器使用该镜像才可删除，否则需删除相关容器才可继续或 -f 强制删除]</td>
</tr>
<tr>
<td>run</td>
<td>Run a command in a new container</td>
<td>创建一个新的容器并运行一个命令</td>
</tr>
<tr>
<td>save</td>
<td>Save an image to a tar archive</td>
<td>保存一个镜像为一个 tar 包 [对应 load]</td>
</tr>
<tr>
<td>search</td>
<td>Search for an image on the Docker Hub</td>
<td>在 docker hub 中搜索镜像</td>
</tr>
<tr>
<td>start</td>
<td>Start a stopped containers</td>
<td>启动容器</td>
</tr>
<tr>
<td>stop</td>
<td>Stop a running containers</td>
<td>停止容器</td>
</tr>
<tr>
<td>tag</td>
<td>Tag an image into a repository</td>
<td>给源中镜像打标签</td>
</tr>
<tr>
<td>top</td>
<td>Lookup the running processes of a container</td>
<td>查看容器中运行的进程信息</td>
</tr>
<tr>
<td>unpause</td>
<td>Unpause a paused container</td>
<td>取消暂停容器</td>
</tr>
<tr>
<td>version</td>
<td>Show the docker version information</td>
<td>查看 docker 版本号</td>
</tr>
</tbody></table>

    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Horsen Liu
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://horsenliu.gitee.io/blog/posts/f629b0e6.html" title="03_常用命令">http://horsenliu.gitee.io/blog/posts/f629b0e6.html</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/blog/tags/Docker/" rel="tag"><i class="fa fa-tag"></i> Docker</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/posts/5e244617.html" rel="prev" title="02_Docker 安装">
      <i class="fa fa-chevron-left"></i> 02_Docker 安装
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/posts/6d6a514d.html" rel="next" title="04_练习部署 nginx & tomcat">
      04_练习部署 nginx & tomcat <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E4%BF%A1%E6%81%AF%E5%91%BD%E4%BB%A4"><span class="nav-text">1. 信息命令</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-1-%E6%9F%A5%E7%9C%8B%E7%89%88%E6%9C%AC-version"><span class="nav-text">1.1 查看版本 version</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-%E6%9F%A5%E7%9C%8B%E7%B3%BB%E7%BB%9F%E4%BF%A1%E6%81%AF-info"><span class="nav-text">1.2 查看系统信息 info</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-%E8%8E%B7%E5%8F%96%E5%B8%AE%E5%8A%A9-help"><span class="nav-text">1.3 获取帮助 help</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E9%95%9C%E5%83%8F%E5%91%BD%E4%BB%A4"><span class="nav-text">2. 镜像命令</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-%E5%88%97%E5%87%BA%E6%9C%AC%E5%9C%B0%E4%B8%BB%E6%9C%BA%E4%B8%8A%E7%9A%84%E9%95%9C%E5%83%8F-images"><span class="nav-text">2.1 列出本地主机上的镜像 images</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-%E6%90%9C%E7%B4%A2%E9%95%9C%E5%83%8F-search"><span class="nav-text">2.2 搜索镜像 search</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-%E4%B8%8B%E8%BD%BD%E9%95%9C%E5%83%8F-pull"><span class="nav-text">2.3 下载镜像 pull</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-4-%E5%88%A0%E9%99%A4%E9%95%9C%E5%83%8F-rmi"><span class="nav-text">2.4 删除镜像 rmi</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E5%AE%B9%E5%99%A8%E5%91%BD%E4%BB%A4"><span class="nav-text">3. 容器命令</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-%E6%96%B0%E5%BB%BA%E5%AE%B9%E5%99%A8%E5%B9%B6%E5%90%AF%E5%8A%A8-run"><span class="nav-text">3.1 新建容器并启动 run</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-%E5%88%97%E5%87%BA%E8%BF%90%E8%A1%8C%E4%B8%AD%E7%9A%84%E5%AE%B9%E5%99%A8-ps"><span class="nav-text">3.2 列出运行中的容器 ps</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-3-%E9%80%80%E5%87%BA%E5%AE%B9%E5%99%A8-exit"><span class="nav-text">3.3 退出容器 exit</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-4-%E8%BF%9B%E5%85%A5%E6%AD%A3%E5%9C%A8%E8%BF%90%E8%A1%8C%E7%9A%84%E5%AE%B9%E5%99%A8-exec-attach"><span class="nav-text">3.4 进入正在运行的容器 exec attach</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#3-4-1-%E6%96%B9%E5%BC%8F%E4%B8%80%EF%BC%9A%E5%9C%A8%E5%AE%B9%E5%99%A8%E4%B8%AD%E6%89%93%E5%BC%80%E6%96%B0%E7%9A%84%E7%BB%88%E7%AB%AF%EF%BC%88%E5%90%AF%E5%8A%A8%E6%96%B0%E7%9A%84%E8%BF%9B%E7%A8%8B%EF%BC%89"><span class="nav-text">3.4.1 方式一：在容器中打开新的终端（启动新的进程）</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-4-2-%E6%96%B9%E5%BC%8F%E4%BA%8C%EF%BC%9A%E7%9B%B4%E6%8E%A5%E8%BF%9B%E5%85%A5%E5%AE%B9%E5%99%A8%E5%90%AF%E5%8A%A8%E5%91%BD%E4%BB%A4%E7%9A%84%E7%BB%88%E7%AB%AF%EF%BC%88%E4%B8%8D%E5%90%AF%E5%8A%A8%E6%96%B0%E7%9A%84%E8%BF%9B%E7%A8%8B%EF%BC%89"><span class="nav-text">3.4.2 方式二：直接进入容器启动命令的终端（不启动新的进程）</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-5-%E5%90%AF%E5%8A%A8-x2F-%E5%81%9C%E6%AD%A2%E5%AE%B9%E5%99%A8-start"><span class="nav-text">3.5 启动&#x2F;停止容器 start</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-6-%E5%88%A0%E9%99%A4%E5%AE%B9%E5%99%A8-rm"><span class="nav-text">3.6 删除容器 rm</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-%E5%85%B6%E4%BB%96%E5%91%BD%E4%BB%A4"><span class="nav-text">4. 其他命令</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-1-%E5%90%8E%E5%8F%B0%E5%90%AF%E5%8A%A8%E5%AE%B9%E5%99%A8"><span class="nav-text">4.1 后台启动容器</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-%E6%9F%A5%E7%9C%8B%E6%97%A5%E5%BF%97-logs"><span class="nav-text">4.2 查看日志 logs</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-3-%E6%9F%A5%E7%9C%8B%E5%AE%B9%E5%99%A8%E4%B8%AD%E8%BF%90%E8%A1%8C%E7%9A%84%E8%BF%9B%E7%A8%8B%E4%BF%A1%E6%81%AF-top"><span class="nav-text">4.3 查看容器中运行的进程信息 top</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-4-%E6%9F%A5%E7%9C%8B%E5%AE%B9%E5%99%A8-x2F-%E9%95%9C%E5%83%8F%E7%9A%84%E5%85%83%E6%95%B0%E6%8D%AE-inspect"><span class="nav-text">4.4 查看容器&#x2F;镜像的元数据 inspect</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-5-%E4%BB%8E%E5%AE%B9%E5%99%A8%E5%86%85%E6%8B%B7%E8%B4%9D%E6%96%87%E4%BB%B6%E5%88%B0%E4%B8%BB%E6%9C%BA-cp"><span class="nav-text">4.5 从容器内拷贝文件到主机 cp</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-%E5%B0%8F%E7%BB%93"><span class="nav-text">5. 小结</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Horsen Liu"
      src="/blog/images/avatar.png">
  <p class="site-author-name" itemprop="name">Horsen Liu</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">220</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">36</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">123</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/horsenliu" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;horsenliu" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://wpa.qq.com/msgrd?v=3&uin=1468224254&site=qq&menu=yes" title="QQ → https:&#x2F;&#x2F;wpa.qq.com&#x2F;msgrd?v&#x3D;3&amp;uin&#x3D;1468224254&amp;site&#x3D;qq&amp;menu&#x3D;yes" rel="noopener" target="_blank"><i class="fab fa-qq fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://sm.ms/image/jyArGeatQ9Plw4n" title="WeChat → https:&#x2F;&#x2F;sm.ms&#x2F;image&#x2F;jyArGeatQ9Plw4n" rel="noopener" target="_blank"><i class="fab fa-weixin fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:horsenliu@qq.com" title="E-Mail → mailto:horsenliu@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i></a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/blog/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Horsen Liu</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">707k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">10:43</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div><script color="252,100,35" pointColor="252,100,35" opacity="1" zIndex="-1" count="150" src="https://cdn.jsdelivr.net/npm/canvas-nest.js@1/dist/canvas-nest.js"></script>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>

<script src="/blog/js/bookmark.js"></script>




  




  
<script src="/blog/js/local-search.js"></script>













  

  

<script src="/blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"scale":1,"hHeadPos":0.5,"vHeadPos":0.618,"jsonPath":"/blog/live2dw/assets/assets/hijiki.model.json"},"display":{"superSample":2,"width":180,"height":360,"position":"left","hOffset":40,"vOffset":-60},"mobile":{"show":false,"scale":0.1},"react":{"opacityDefault":0.7,"opacityOnHover":0.8},"log":false});</script></body>
</html>
